package com.ebeijia.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import oracle.jdbc.OracleCallableStatement;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ebeijia.dto.HomSourceDto;
import com.ebeijia.dto.RcvblUserInfo;
import com.ebeijia.exception.ServiceException;
import com.ebeijia.mybatis.pojo.ApplyInfo;
import com.ebeijia.mybatis.pojo.HomeSourceInfo;
import com.ebeijia.mybatis.pojo.OrderInfo;
import com.ebeijia.mybatis.pojo.UserInfo;
import com.ebeijia.service.user.HomeService;
import com.ebeijia.service.user.UserService;
import com.ebeijia.util.DateUtil;
import com.ebeijia.util.LoggerUtil;
import com.ebeijia.web.ResponseMessage;

@Controller
@RequestMapping("/home")
public class HomesController {

	@Autowired
	private HomeService homeSrvice;

	@Autowired
	private UserService userService;

	/***
	 * 根据用户Id查询 绑定房源
	 * 
	 * @param userId
	 * @param request
	 * @return
	 */
	@RequestMapping("homesInfo")
	@ResponseBody
	public ResponseMessage getUserFindHomesResouces(String userId,
			HttpServletRequest request) {
		// 两种方法判断 1.用boolean 返回检查true 和false
		// 2、检查查询激励返回是否为 null
		userId = request.getParameter("userId");
		if (!userId.equals("")) {
			List<HomeSourceInfo> home = homeSrvice.getUserAndHomeById(userId);
			if (home != null) {
				//LoggerUtil.info("绑定房源成功 ");
				// 执行后续步骤
				return ResponseMessage.success(home);

			} else {
				return ResponseMessage.error("1", "没有此用户");
			}
		} else {
			LoggerUtil.info("userId不允许为空请输入");
		}
		Map map = new HashMap();
		// map.put(key, value)
		return null;
	}

	@RequestMapping("queryExistUser")
	@ResponseBody
	public ResponseMessage queryExistUserFromWater(String userNo,
			HttpServletRequest request) throws ServiceException {
		HomSourceDto homeSouceDto = userService.queryUserInfoFromPkg(userNo);
		if (homeSouceDto == null) {
			return ResponseMessage.error("用户编号不存在");
		}

		return ResponseMessage.success(homeSouceDto);
	}

	/***
	 * 根据房源号查询
	 * 
	 * @param homeUserNo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("gethomeUserNo")
	@ResponseBody
	public ResponseMessage getUserByName(String homeUserNo,
			HttpServletRequest request) throws Exception {
		if (homeUserNo != null) {
			HomSourceDto homeSourceDto = userService
					.queryUserInfoFromPkg(homeUserNo);
			homeSourceDto.setLastDate(homeSourceDto.getLastProcessTime() == null ? ""
							: DateUtil.formatDate(
									homeSourceDto.getLastProcessTime(),
									"yyyy-MM-dd"));
			return ResponseMessage.success(homeSourceDto);
		}
		return null;
	}

	/**
	 * 新增房源信息
	 * 
	 * @param homeInfo
	 * @param request
	 * @return
	 */
	@RequestMapping("/addhome")
	@ResponseBody
	public ResponseMessage addHome(UserInfo userInfo, HttpServletRequest request)
			throws Exception {
		if (StringUtils.isEmpty(userInfo.getUserNo())) {
			return ResponseMessage.error("用户编号不能为空");
		}

		if (StringUtils.isEmpty(userInfo.getPhoneNo())) {
			return ResponseMessage.error("手机号码不可以为空");
		}
		/*if (StringUtils.isEmpty(userInfo.getIdNo())) {
			return ResponseMessage.error("身份证号不可以为空");
		}*/
		return userService.addNewHomeSource(userInfo);

	}

	/**
	 * 用户销户 根据 homeUserNO
	 * 
	 * @param homeUserNo
	 * @return
	 */
	@RequestMapping("/deleteByUser")
	@ResponseBody
	public ResponseMessage deleteByUser(String homeUserNo,
			HttpServletRequest request) {
		homeUserNo = request.getParameter("homeUserNo");
		int homeinfo = homeSrvice.destoryUser(homeUserNo);
		if (homeinfo != 0) {
			return ResponseMessage.error("0001", "销户成功");
		}
		return ResponseMessage.error("0002", "销户失败");
	}

	/**
	 * 调用接口 水量查询
	 * 
	 * @param homeNameNo
	 * @param startDate
	 * @param endDate
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/queryWaterList")
	@ResponseBody
	public ResponseMessage queryWaterList(Map<String, Object> map,
			HttpServletRequest request) throws Exception {
		String startDate = request.getParameter("USER_AGE");
		String endDate = request.getParameter("USER_END");
		String homeNameNo = request.getParameter("userNoHome");
	/*	if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
			Calendar cal = Calendar.getInstance();
			Date endDateTmp=cal.getTime();
			cal.add(Calendar.MONTH, -3);
			Date startDateTmp=cal.getTime();
			startDate=DateUtil.formatDate(startDateTmp, "yyyy-MM");
			endDate=DateUtil.formatDate(endDateTmp, "yyyy-MM");

		}*/
		map.put("userNo", homeNameNo);
		map.put("startYearMonth", startDate);
		map.put("endYearMonth", endDate);
		homeSrvice.getWaterList(map);
		String result = (String) map.get("result");
		List<Object> list = (List<Object>) map.get("rcvblDet");
		Map resultMap=new HashMap<>();
		resultMap.put("startDate", startDate);
		resultMap.put("endDate", endDate);
		resultMap.put("waterList", list);
			return ResponseMessage.success(resultMap);


	}

	/**
	 * 调用 接口 消费记录查询
	 * 
	 * @param homeNameNo
	 * @param startDate
	 * @param endDate
	 * @param request
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/queryChargeDetail")
	@ResponseBody
	public ResponseMessage queryChargeDetailProc(Map<String, Object> map,
			HttpServletRequest request) throws Exception {
		String startDate = request.getParameter("starttime");
		String endDate = request.getParameter("endtime");
		String homeNameNo = request.getParameter("userNoHome");
		// Map<String, Object> map = new HashMap<String, Object>();

		if (StringUtils.isEmpty(startDate) || StringUtils.isEmpty(endDate)) {
			Calendar cal = Calendar.getInstance();
			Date endDateTmp=cal.getTime();
			cal.add(Calendar.MONTH, -3);
			Date startDateTmp=cal.getTime();
			startDate=DateUtil.formatDate(startDateTmp, "yyyy-MM");
			endDate=DateUtil.formatDate(endDateTmp, "yyyy-MM");

		}
		map.put("userNo", homeNameNo);
		map.put("startYearMonth", startDate);
		map.put("endYearMonth", endDate);
		homeSrvice.getBalanceList(map);
		String result = (String) map.get("result");
		List<Object> list = (List<Object>) map.get("rcvblDet");
		Map resultMap=new HashMap<>();
		resultMap.put("startDate", startDate);
		resultMap.put("endDate", endDate);
		resultMap.put("payList", list);
		return ResponseMessage.success(resultMap);

	}

	/**
	 * 调用接口 余额查询
	 * 
	 * @param homeNameNo
	 * @param startDate
	 * @param endDate
	 * @param request
	 * @return
	 */
	@RequestMapping("/queryFreeAndWater")
	@ResponseBody
	public ResponseMessage queryFreeAndWater(Map<String, Object> map,
			HttpServletRequest request) {
		RcvblUserInfo info = new RcvblUserInfo();
		String phone = request.getParameter("phone");
		String identity = request.getParameter("identity");
		String homeNameNo = request.getParameter("userNoHome");
		map.put("USERCODE", homeNameNo);
		map.put("IDCARDND", identity);
		map.put("PHONENO", phone);
		homeSrvice.getBanlanceFree(map);

		Integer result = (Integer) map.get("result");
		String areaName = (String) map.get("AREA_NAME");
		String homeNumber = (String) map.get("AREA_NAME");
		String buldingName = (String) map.get("BUILDING_NAME");
		String areaAddress = (String) map.get("AREA_ADDRESS");
		String userName = (String) map.get("USER_NAME");
		Integer balanceAmt = (Integer) map.get("BALANCE_AMT");
		Integer lastTotalFlux = (Integer) map.get("LAST_TOTAL_FLUX");
		Date lastProcessTime = (Date) map.get("LAST_PROCESS_TIME");
		info.setResult(result);
		info.setAreaName(areaName);
		info.setHouseNumber(homeNumber);
		info.setBuildingName(buldingName);
		info.setAreaAddress(areaAddress);
		info.setUserName(userName);
		info.setBalanceAmt(balanceAmt);
		info.setLastProcessTime(lastProcessTime);
		info.setLastTotalFlux(lastTotalFlux);

		/*
		 * SimpleDateFormat date = new SimpleDateFormat("YYYY-MM"); startDate =
		 * date.format(strat); endDate = date.format(end);
		 */
		if (result == 0) {
			return ResponseMessage.success(info);
		} else {
			String msg = "没有数据信息，请正确填写";
			return ResponseMessage.error("0002", msg);
		}

	}

	/**
	 * 根据房源UserNo查询
	 * 
	 * @param homeNameNo
	 */
	@RequestMapping("/selectUserNo")
	@ResponseBody
	public ResponseMessage selectUserNo(String homeUserNo, String userId,
			HttpServletRequest request) {

		HomeSourceInfo home = homeSrvice.getHomeUserNo(homeUserNo, userId);
		if (home != null) {
			return ResponseMessage.success(home);
		} else {
			return ResponseMessage.error("没有此用户编号请重新输入！");
		}
	}
}
